package com.baselibrary.bigimage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jye on 2017/9/20.
 */
public class ImageLoadActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.viewPager)
    FixedViewPager viewPager;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    private int imagePosition;
    private List<ImageLoadFragment> imageLoadFragments;
    private List<String> urlList;
    private ImageViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_imageload);
        ButterKnife.bind(this);
        initIntent();
        initView();
        setViewInfo();
    }

    private void initIntent() {

        urlList = getIntent().getStringArrayListExtra("urlList");
        imagePosition = getIntent().getIntExtra("image_position", 0);
    }

    private void initView() {

        int space = getResources().getDimensionPixelSize(R.dimen.space_item);
        viewPager.setPageMargin(space);
        viewPager.addOnPageChangeListener(this);
    }

    private void setViewInfo() {

        if (urlList != null) {
            if (imagePosition < 1) {
                tvTip.setVisibility(View.GONE);
            } else {
                tvTip.setVisibility(View.VISIBLE);

                tvTip.setText((imagePosition + 1) + "/" + urlList.size());
            }
            if (imageLoadFragments == null) {
                imageLoadFragments = new ArrayList<>();
            }
            imageLoadFragments.clear();
            ImageLoadFragment fragment;
            for (String str : urlList) {
                if (str != null) {
                    fragment = ImageLoadFragment.newInstance(str);
                    imageLoadFragments.add(fragment);
                }
            }
        }
        viewPagerAdapter = new ImageViewPagerAdapter(getSupportFragmentManager(), imageLoadFragments);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(imagePosition);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvTip.setText((position + 1) + "/" + urlList.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return super.onTouchEvent(event);
    }

    class ImageViewPagerAdapter extends FragmentPagerAdapter {

        private List<ImageLoadFragment> fragmentList = new ArrayList<>();

        public ImageViewPagerAdapter(FragmentManager fm, List<ImageLoadFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            return super.instantiateItem(container, position);
        }
    }
}
